package com.artron.mediaartron.ui.fragment.center;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.artron.baselib.adapter.recyclerview.BaseViewHolder;
import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.entity.Response;
import com.artron.baselib.image.ImageUtils;
import com.artron.baselib.utils.ToastUtil;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.config.AppProfile;
import com.artron.mediaartron.data.entity.WorksData.WorksListBean;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.net.api.HostApi;
import com.artron.mediaartron.ui.fragment.center.WorksFinishFragment.ContentType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-13.
 */

public class WorksFinishDetailFragment extends BaseStaticFragment {


    @BindView(R.id.WorksFinishDetailFragment_tv_name)
    protected TextView mTvName;
    @BindView(R.id.WorksFinishDetailFragment_ll_container)
    protected LinearLayout mLlContainer;
    @BindView(R.id.WorksFinishDetailFragment_ll_content)
    protected LinearLayout mLlContent;
    @BindView(R.id.WorksFinishDetailFragment_fl_image)
    protected FrameLayout mFlImage;
    @BindView(R.id.WorksFinishDetailFragment_iv_frame)
    protected ImageView mIvFrame;
    @BindView(R.id.SWorksFinishDetailFragment_iv_image)
    protected ImageView mIvImage;
    @BindView(R.id.WorksFinishDetailFragment_btn_buy_again)
    protected Button mBtnBuyAgain;
    private WorksListBean mFragmentData;
    private int mDrawableId;

    public static WorksFinishDetailFragment newInstance(WorksListBean bean) {
        WorksFinishDetailFragment fragment = new WorksFinishDetailFragment();
        fragment.init(bean);
        return fragment;
    }

    private void init(WorksListBean bean) {
        String[] typeCodeArray = UIUtils.getStringArray(R.array.FrameShapeType);
        for (int i = 0; i < typeCodeArray.length; i++) {
            if (typeCodeArray[i].equals(bean.getShape())) {
                mDrawableId = UIUtils.getIdentifier("ic_frame_shape_type" + i, "drawable");
                break;
            }
        }
        this.mFragmentData = bean;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_works_finish_detail;
    }

    @Override
    protected void initView() {
        mTvName.setText(mFragmentData.getName());

        mBtnBuyAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HostApi api = RetrofitHelper.getHostApi();
                String passId = AppProfile.getUserInfo().getPassId();
                RetrofitHelper.subscript(api.saveTrolleyDetail(passId, mFragmentData.getId(), 1, mFragmentData.getSkuCode(),
                        "artron", "android"), new Action1<Response>() {
                    @Override
                    public void call(Response response) {
                        ToastUtil.show(response.getMessage());
                    }
                });
            }
        });
        initImageWidthAndHeight();

        List<String> contentList = buildValueList();
        List<String> typeList = buildTypeList();

        for (int i = 0; i < typeList.size(); i++) {
            BaseViewHolder holder = BaseViewHolder.createViewHolder(mContext, mLlContent,
                    R.layout.layout_works_finish_detail_content);
            holder.setText(R.id.WorksFinishDetailFragment_tv_value, contentList.get(i))
                .setText(R.id.WorksFinishDetailFragment_tv_type, typeList.get(i));
            mLlContent.addView(holder.getContentView());
        }
        mLlContent.addView(new TextView(mContext));

        mLlContent.post(new Runnable() {
            @Override
            public void run() {
                int parentHeight = mLlContainer.getHeight();
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mTvName.getLayoutParams();
                int marginHeight = layoutParams.topMargin + layoutParams.bottomMargin;
                layoutParams = (LinearLayout.LayoutParams) mFlImage.getLayoutParams();
                marginHeight += layoutParams.topMargin + layoutParams.bottomMargin;
                layoutParams = (LinearLayout.LayoutParams) mLlContent.getLayoutParams();
                marginHeight += layoutParams.topMargin;
                int contentHeight = parentHeight - (mTvName.getHeight() + mFlImage.getHeight() + marginHeight);
                mLlContent.setMinimumHeight(contentHeight + 120);
            }
        });


        ImageUtils.setUrl(mIvImage, mFragmentData.getThumbnailPath());
    }

    private List<String> buildValueList() {
        List<String> list = new ArrayList<>();
        for (ContentType contentType : ContentType.values()) {
            if (contentType.getType().equals(mFragmentData.getTypeCode())) {
                list.add(contentType.name());
                break;
            }
        }

        list.add("外观尺寸".equals(mFragmentData.getShape() ) ? "" : mFragmentData.getShape());
        list.add(mFragmentData.getSize() + "mm");
        list.add(mFragmentData.getCreateTime());
        return list;
    }

    private List<String> buildTypeList() {
        return Arrays.asList("产品类型", "框型", "尺寸", "创作时间");
    }

    public void initImageWidthAndHeight() {
        float widthPixels = getResources().getDisplayMetrics().widthPixels;
        float per = widthPixels / 800.f;

        String[] sizeArr = mFragmentData.getSize().split("X");
        int width = Integer.parseInt(sizeArr[0]);
        int height = Integer.parseInt(sizeArr[1]);

        if ("haibao".equals(mFragmentData.getTypeCode())) {
            ViewGroup.LayoutParams frameParams = mIvFrame.getLayoutParams();
            frameParams.height = (int) (per * height - 10.f);
            frameParams.width = (int) (per * width - 10.f);
            mIvImage.setLayoutParams(frameParams);
            mIvFrame.setVisibility(View.GONE);
        } else {
            ViewGroup.LayoutParams frameParams = mIvFrame.getLayoutParams();
            frameParams.height = (int) (per * height);
            frameParams.width = (int) (per * width);
            mIvFrame.setLayoutParams(frameParams);
            mIvFrame.setImageResource(mDrawableId);
            mIvFrame.setVisibility(View.VISIBLE);


            ViewGroup.LayoutParams imageParams = mIvImage.getLayoutParams();
            imageParams.height = (int) ((height - 120) * per);
            imageParams.width = (int) ((width - 120) * per);
            mIvImage.setLayoutParams(imageParams);
        }
    }

}
